<?php

ob_start();
session_start();
include_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/configs/database.php');
/*

  /* Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/libraries/PHPExcel_1.7.9_doc/Classes/PHPExcel.php');

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
        ->setLastModifiedBy("Maarten Balliauw")
        ->setTitle("Office 2007 XLSX Test Document")
        ->setSubject("Office 2007 XLSX Test Document")
        ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
        ->setKeywords("office 2007 openxml php")
        ->setCategory("Test result file");
// Lay du lieu thong ke


$madv = $_SESSION['madv'];
$tinhtrang = $_SESSION['tinhtrang'];

if ($madv != 'tatca') {


    $db = new database();
    $db->setQuery("select * from don_vi where MA_DON_VI = '" . $madv . "'");
    $re = $db->fetchAll();
    $row = mysql_fetch_array($re);
    $tendv = $row['TEN_DON_VI'];

    //Danh sach may tinh so huu
    $db->setQuery("select * from chi_tiet_so_huu where MA_DON_VI = '" . $madv . "' group by SO_N0");
    $dong = $db->fetchAll();

    $mang = array();
    $i = 0;
    while ($in = mysql_fetch_array($dong)) {
        $mang[$i] = $in['SO_N0'];
        $i++;
    }

    $mang2 = array();
    //Lay danh sach may don vi dang so huu	
    $k = 0;
    for ($j = 0; $j < $i; $j++) {//vong for 1
        $db->setQuery("select * from chi_tiet_so_huu where SO_N0 = '" . $mang[$j] . "' order by NGAY_CHUYEN_DEN desc");
        $truyvan = $db->fetchAll();
        $madvtimdc = mysql_fetch_array($truyvan);

        if ($madvtimdc['MA_DON_VI'] == $madv) {

            $mang2[$k] = $madvtimdc['SO_N0'];

            $k++;
        }
    }//ket thuc for 1
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A1', 'Công Ty Điện Lực Cà Mau');

            $objPHPExcel->getActiveSheet()->mergeCells('A1:D1');
			$objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('G1', 'CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM');
			$objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('G2', 'Độc Lập - Tự Do - Hạnh Phúc');

            $objPHPExcel->getActiveSheet()->mergeCells('G1:J1');
			$objPHPExcel->getActiveSheet()->mergeCells('G2:J2');
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A2', 'Địa chỉ: 22 Ngô Quyền P2 TP Cà Mau');
			
            $objPHPExcel->getActiveSheet()->getStyle('A1:J2')->applyFromArray(
                    array(
                        'font' => array(
                            'size' => 13,
                            'bold' => true
									)
						)
            );
			$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->applyFromArray(
                    array(
                        'font' => array(
                            'size' => 12,
							'italic' => true

                        ),
                    )
            );
						$objPHPExcel->getActiveSheet()->getStyle('A1:J2')->applyFromArray(
                    array(
                           'alignment' => array(
                            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                            'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                        )
                    )
            );
            $objPHPExcel->getActiveSheet()->mergeCells('A2:D2');
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('I4', 'Ngày Tạo: ');
			$objPHPExcel->getActiveSheet()->getStyle('I4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			$objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('J4', date('d/m/Y', time()));
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A4', 'Liên hệ: Nguyễn Phạm Thanh Tú/CBKT/Phòng KH-KT');
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A5', 'Điện thoại/Fax/Email: ');
            $objPHPExcel->getActiveSheet()->getStyle('A4:O5')->applyFromArray(
                    array(
                        'font' => array(
                            'size' => 12
                        //'bold' => true
                        ),
                        'alignment' => array(
                            'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
                        )
                    )
            );

    if (count($mang2) == 0) { // Xét xem có sở hữu máy biến áp ko
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A6', 'Đơn vị ' . $tendv . ' không sở hữu máy biến áp nào để tạo báo cáo');
    } else {
        // Trường hợp là một tình trạng cụ thể
        if ($tinhtrang != 'khongco') {

            $mang3 = array();
            $dem = 0;
            for ($i = 0; $i < count($mang2); $i++) { //vong for 2
                $db->setQuery("select * from chi_tiet_tt_may where SO_N0 = '" . $mang2[$i] . "' order by TINH_TRANG_NGAY desc");
                $chitiettt = $db->fetchAll();

                $ketqua = mysql_fetch_array($chitiettt);
                if (strtolower($ketqua['TEN_TINH_TRANG_MAY']) == strtolower($tinhtrang)) {
                    $mang3[$dem] = $ketqua['SO_N0'];

                    $dem++;
                }
            }//ket thuc for 2


            if (count($mang3) == 0) { //Kiểm tra đơn vị này ko có máy nào có tình trạng vừa chọn
                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A6', 'Đơn vị ' . $tendv . ' không có máy nào ' . $tinhtrang);
            } else {


                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A6', 'Bảng Báo Cáo Theo Tình Trạng ' . $tinhtrang. ' (dữ liệu mới nhất)')
                        ->setCellValue('A7', 'Tên Đơn Vị: ' . $tendv)
                        ->setCellValue('A8', 'STT')
                        ->setCellValue('B8', 'MSTS')
                        ->setCellValue('C8', 'Số N0')
                        ->setCellValue('D8', 'Ngày Chuyển Đến')
                        ->setCellValue('E8', 'S (kVA)')
                        ->setCellValue('F8', 'U (kV)')
						->setCellValue('G8', 'Chiều Dài (mm)')
						->setCellValue('H8', 'Chiều Rộng (mm)')
						->setCellValue('I8', 'Chiều Cao (mm)')						
						->setCellValue('J8', 'Ruột Máy (kg)')
						->setCellValue('K8', 'Dầu (kg)')
						->setCellValue('L8', 'Tổng (kg)')
						->setCellValue('M8', 'Loại Dầu')
						->setCellValue('N8', 'Mã Trạm')
                        ->setCellValue('O8', 'Tên Trạm')
						->setCellValue('P8', 'Địa Chỉ Trạm')
                        ->setCellValue('Q8', 'Ngày Vận Hành')
						->setCellValue('R8', 'Ngày Ngừng Vận Hành')
                        ->setCellValue('S8', 'Năm Sản Xuất')
                        ->setCellValue('T8', 'Nhà Sản Xuất')
						->setCellValue('U8', 'Nước Sản Xuất')
                        ->setCellValue('V8', 'Loại Máy')
                        ->setCellValue('W8', 'Tình Trạng Ngày')
                        ->setCellValue('X8', 'Chi Tiết Tình Trạng')
                        ->setCellValue('Y8', 'Ngày Dại Tu')
						->setCellValue('Z8', 'Năm Nhập Về')
                        ->setCellValue('AA8', 'Thông Số Đo')
                        ->setCellValue('AB8', 'Nhận Xét')
                        ->setCellValue('AC8', 'Ghi Chú');


                $stt = 1;
                $cellcount = 9;
                for ($i = 0; $i < count($mang3); $i++) {
                    $db->setQuery("select * from may_bien_ap as mba, loai_may as loai , nha_sx as nsx, loai_dien_ap as loaida where mba.SO_N0 = '" . $mang3[$i] . "' and mba.MA_NSX = nsx.MA_NSX and loai.MA_LOAI = mba.MA_LOAI and loaida.MA_DIEN_AP = mba.MA_DIEN_AP");
                    $truyvan = $db->fetchAll();
					if (mysql_num_rows($truyvan) != 0) {
                    $kqtv = mysql_fetch_array($truyvan);


                    $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('A' . $cellcount, $stt);
                    $stt++; //1
                    //kt msts có null hay ko
                    if ($kqtv['MSTS'] == NULL)
                        $kqtv['MSTS'] = 'Kc';
					  //lay ngay chuyen den
					$ngaychuyenden = 'Kc';
                    $db->setQuery("select * from chi_tiet_so_huu where SO_N0 = '" . $mang3[$i] . "' order by NGAY_CHUYEN_DEN desc");
                    $truyvansohuu = $db->fetchAll();
                    $kqtvsh = mysql_fetch_array($truyvansohuu);
					if ($kqtvsh['NGAY_CHUYEN_DEN'] !=  NULL) {
						$ngaychuyenden = date('d-m-Y', strtotime($kqtvsh['NGAY_CHUYEN_DEN']));
					}
					if ($kqtv['CONG_SUAT'] == 0)            $kqtv['CONG_SUAT'] = 'Kc';	
					if ($kqtv['MA_DIEN_AP'] == 0)            $kqtv['CAP_DIEN_AP'] = 'Kc';
					if ($kqtv['CHIEU_DAI'] == 0)            $kqtv['CHIEU_DAI'] = 'Kc';	
					if ($kqtv['CHIEU_RONG'] == 0)            $kqtv['CHIEU_RONG'] = 'Kc';	
					if ($kqtv['CHIEU_CAO'] == 0)            $kqtv['CHIEU_CAO'] = 'Kc';
					if ($kqtv['RUOT_MAY'] == 0)            $kqtv['RUOT_MAY'] = 'Kc';
					if ($kqtv['DAU_MAY'] == 0)            $kqtv['DAU_MAY'] = 'Kc';
					if ($kqtv['TONG'] == 0)            $kqtv['TONG'] = 'Kc';
					if ($kqtv['TEN_LOAI_DAU'] == NULL)            $kqtv['TEN_LOAI_DAU'] = 'Kc';
					 //lay thong tin tram
					$ngayvanhanh = 'Kc';
					$diachitram = 'Kc';
                    $db->setQuery("select * from mba_tram as ct, tram as t where ct.SO_N0 = '" . $mang3[$i] . "' and ct.MA_TRAM = t.MA_TRAM order by ct.NGAY_VAN_HANH desc");
                    $truyvantram = $db->fetchAll();
                    $kqtvtram = mysql_fetch_array($truyvantram);
                    if ($kqtvtram['TEN_TRAM'] == NULL) {
                        $kqtvtram['MA_TRAM'] = 'Chưa lên trạm';
						$kqtvtram['TEN_TRAM'] = 'Kc';
						$ngayvanhanh = 'Kc';
					}
					else {
							 $diachitram = $kqtvtram['DIA_CHI_TRAM'];
							$ngayvanhanh = date('d-m-Y', strtotime($kqtvtram['NGAY_VAN_HANH']));
					}
					//lay thong tin ngung van hanh
					 $ngayngungvh = 'Kc';
                    $db->setQuery("select * from chi_tiet_ngung_van_hanh as ct where ct.SO_N0 = '" . $mang3[$i] . "' order by ct.NGAY_NGUNG_VH desc");
                    $truyvannvh = $db->fetchAll();
                    $kqtvnvh = mysql_fetch_array($truyvannvh);
                    if ($kqtvnvh['NGAY_NGUNG_VH'] != NULL)      
					 {
						$ngayngungvh = date('d-m-Y', strtotime($kqtvnvh['NGAY_NGUNG_VH']));
					}
					if ($kqtv['NAM_SX'] == NULL)         $kqtv['NAM_SX'] = 'Kc';
					if ($kqtv['TEN_NSX'] == NULL)         $kqtv['TEN_NSX'] = 'Kc';
					if ($kqtv['TEN_NUOC'] == NULL)         $kqtv['TEN_NUOC'] = 'Kc';
					if ($kqtv['TEN_LOAI'] == NULL)         $kqtv['TEN_LOAI'] = 'Kc';
					$db->setQuery("select * from chi_tiet_tt_may where SO_N0 = '" . $mang3[$i] . "' order by TINH_TRANG_NGAY desc");
                    $truyvantt = $db->fetchAll();
                    $kqtvtt = mysql_fetch_array($truyvantt);
					$tinhtrangngay = date('d-m-Y', strtotime($kqtvtt['TINH_TRANG_NGAY']));
					 if ($kqtvtt['DG_TINH_TRANG'] == NULL)
                        $kqtvtt['DG_TINH_TRANG'] = 'Kc';
					 //lay thong tin dai tu
					$ngaydaitu = 'Kc';
                    $db->setQuery("select * from chi_tiet_dai_tu where SO_N0 = '" . $mang3[$i] . "'  order by NGAY_DAI_TU desc");
                    $truyvandaitu = $db->fetchAll();
                    $kqtvdaitu = mysql_fetch_array($truyvandaitu);
                    if ($kqtvdaitu['NGAY_DAI_TU'] != NULL)                     
					{
						$ngaydaitu = date('d-m-Y', strtotime($kqtvdaitu['NGAY_DAI_TU']));
					}
					
					// năm nhập về null ko
                    if ($kqtv['NAM_NHAP_VE'] == NULL)
                        $kqtv['NAM_NHAP_VE'] = 'Kc';
					if ($kqtv['THONG_SO_DO'] == NULL)     $kqtv['THONG_SO_DO'] = 'Kc';
					if ($kqtv['NHAN_XET'] == NULL)        $kqtv['NHAN_XET'] = 'Kc';
					if ($kqtv['GHI_CHU'] == NULL)         $kqtv['GHI_CHU'] = 'Kc';
                    $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('B' . $cellcount, $kqtv['MSTS'])
                            ->setCellValue('C' . $cellcount, $kqtv['SO_N0'])
                            ->setCellValue('D' . $cellcount, $ngaychuyenden)
                            ->setCellValue('E' . $cellcount, $kqtv['CONG_SUAT'])
                            ->setCellValue('F' . $cellcount, $kqtv['CAP_DIEN_AP'])
	                        ->setCellValue('G' . $cellcount, $kqtv['CHIEU_DAI'])
                            ->setCellValue('H' . $cellcount, $kqtv['CHIEU_RONG'])
                            ->setCellValue('I' . $cellcount, $kqtv['CHIEU_CAO'])
                            ->setCellValue('J' . $cellcount, $kqtv['RUOT_MAY'])
                            ->setCellValue('K' . $cellcount, $kqtv['DAU_MAY'])
                            ->setCellValue('L' . $cellcount, $kqtv['TONG'])
                            ->setCellValue('M' . $cellcount, $kqtv['TEN_LOAI_DAU'])
                            ->setCellValue('N' . $cellcount, $kqtvtram['MA_TRAM'])
                            ->setCellValue('O' . $cellcount, $kqtvtram['TEN_TRAM'])
                            ->setCellValue('P' . $cellcount, $diachitram)
                            ->setCellValue('Q' . $cellcount, $ngayvanhanh)
                            ->setCellValue('R' . $cellcount, $ngayngungvh)
							->setCellValue('S' . $cellcount, $kqtv['NAM_SX'])
							->setCellValue('T' . $cellcount, $kqtv['TEN_NSX'])
							->setCellValue('U' . $cellcount, $kqtv['TEN_NUOC'])
							->setCellValue('V' . $cellcount, $kqtv['TEN_LOAI'])
							->setCellValue('W' . $cellcount, $tinhtrangngay)
							->setCellValue('X' . $cellcount, $kqtvtt['DG_TINH_TRANG'])
							->setCellValue('Y' . $cellcount, $ngaydaitu)
							->setCellValue('Z' . $cellcount, $kqtv['NAM_NHAP_VE'])
							->setCellValue('AA' . $cellcount, $kqtv['THONG_SO_DO'])
							->setCellValue('AB' . $cellcount, $kqtv['NHAN_XET'])
							->setCellValue('AC' . $cellcount, $kqtv['GHI_CHU']);
                    $cellcount++;
					}
                }

                $objPHPExcel->getActiveSheet()->mergeCells('A6:AC6');
                $objPHPExcel->getActiveSheet()->mergeCells('A7:AC7');
                $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
                $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('R')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('S')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('T')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('U')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('V')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('W')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('X')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('Y')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('Z')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('AA')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('AB')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('AC')->setAutoSize(true);

                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A' . $cellcount, 'Kí Hiệu   Kc: Không có');
                $objPHPExcel->getActiveSheet()->getStyle('A8:' .
                        $objPHPExcel->getActiveSheet()->getHighestColumn() .
                        $objPHPExcel->getActiveSheet()->getHighestRow())->applyFromArray(
                        array(
                            'font' => array(
                                'size' => 12
                            ),
                            'borders' => array(
                                'allborders' => array(
                                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                                    'color' => array('rgb' => '#000000'),
                                )
                            ),
                            'alignment' => array(
                                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                            )
                        )
                );
// Set format for tableheader
                $objPHPExcel->getActiveSheet()->getStyle('A6:AC7')->applyFromArray(
                        array(
                            'font' => array(
                                'bold' => true,
                                'size' => 13                             
                            ),
                            'alignment' => array(

                                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                            )
                        )
                );
                $objPHPExcel->getActiveSheet()->mergeCells('A' . $cellcount . ':D' . $cellcount);
            } //kt ko co tinh trạng bao cao
        }// het if của có tình trạng
        else
        // Chọn tình trạng là không có
        if ($tinhtrang == 'khongco') {
            $dem = 0;
            $mang3 = array();
            for ($i = 0; $i < count($mang2); $i++) { //vong for 2
                $db->setQuery("select * from chi_tiet_tt_may where SO_N0 = '" . $mang2[$i] . "'");
                $chitiettt = $db->fetchAll();
                $ketqua = mysql_fetch_array($chitiettt);
                //$kt = mysqli_num_rows($ketqua);
                if ($ketqua['SO_N0'] == NULL) {
                    $mang3[$dem] = $mang2[$i];
                    $dem++;
                }
            }


            //Bat dau xu ly de tao bao cao

            if (count($mang3) == 0) { // Kiểm tra đơn vị có máy nào không có tình trạng hay ko
                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A6', 'Đơn vị ' . $tendv . ' không có máy nào không có tình trạng');
                //echo "Đơn vị ".$tendv." không có máy không có tình trạng ";
            } else {
$objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A6', 'Bảng Báo Cáo Cho Các Máy Chưa Nhập Tình Trạng (dữ liệu mới nhất)')
                        ->setCellValue('A7', 'Tên Đơn Vị: ' . $tendv)
                        ->setCellValue('A8', 'STT')
                        ->setCellValue('B8', 'MSTS')
                        ->setCellValue('C8', 'Số N0')
                        ->setCellValue('D8', 'Ngày Chuyển Đến')
                        ->setCellValue('E8', 'S (kVA)')
                        ->setCellValue('F8', 'U (kV)')
						->setCellValue('G8', 'Chiều Dài (mm)')
						->setCellValue('H8', 'Chiều Rộng (mm)')
						->setCellValue('I8', 'Chiều Cao (mm)')						
						->setCellValue('J8', 'Ruột Máy (kg)')
						->setCellValue('K8', 'Dầu(kg)')
						->setCellValue('L8', 'Tổng(kg)')
						->setCellValue('M8', 'Loại Dầu')
						->setCellValue('N8', 'Mã Trạm')
                        ->setCellValue('O8', 'Tên Trạm')
						->setCellValue('P8', 'Địa Chỉ Trạm')
                        ->setCellValue('Q8', 'Ngày vận hành')
						->setCellValue('R8', 'Ngày Ngừng Vận Hành')
                        ->setCellValue('S8', 'Năm Sản Xuất')
                        ->setCellValue('T8', 'Nhà Sản Xuất')
						->setCellValue('U8', 'Nước Sản Xuất')
                        ->setCellValue('V8', 'Loại Máy')
                        ->setCellValue('W8', 'Ngày Đại Tu')
						->setCellValue('X8', 'Năm Nhập Về')
                        ->setCellValue('Y8', 'Thông Số Đo')
                        ->setCellValue('Z8', 'Nhận Xét')
                        ->setCellValue('AA8', 'Ghi Chú');
                $stt = 1;
                $cellcount = 9;
                for ($i = 0; $i < count($mang3); $i++) {
                    $db->setQuery("select * from may_bien_ap as mba, loai_may as loai , nha_sx as nsx, loai_dien_ap as loaida where mba.SO_N0 = '" . $mang3[$i] . "' and mba.MA_NSX = nsx.MA_NSX and loai.MA_LOAI = mba.MA_LOAI and loaida.MA_DIEN_AP = mba.MA_DIEN_AP");
                    $truyvan = $db->fetchAll();
					if (mysql_num_rows($truyvan) != 0) {
                    $kqtv = mysql_fetch_array($truyvan);
                    $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('A' . $cellcount, $stt);
                    $stt++; //1
                    //kt msts có null hay ko
                    if ($kqtv['MSTS'] == NULL)
                        $kqtv['MSTS'] = 'Kc';
					  //lay ngay chuyen den
					$ngaychuyenden = 'Kc';
                    $db->setQuery("select * from chi_tiet_so_huu where SO_N0 = '" . $mang3[$i] . "' order by NGAY_CHUYEN_DEN desc");
                    $truyvansohuu = $db->fetchAll();
                    $kqtvsh = mysql_fetch_array($truyvansohuu);
					if ($kqtvsh['NGAY_CHUYEN_DEN'] !=  NULL) {
						$ngaychuyenden = date('d-m-Y', strtotime($kqtvsh['NGAY_CHUYEN_DEN']));
					}
					if ($kqtv['CONG_SUAT'] == 0)            $kqtv['CONG_SUAT'] = 'Kc';	
					if ($kqtv['MA_DIEN_AP'] == 0)            $kqtv['CAP_DIEN_AP'] = 'Kc';
					if ($kqtv['CONG_SUAT'] == 0)            $kqtv['CONG_SUAT'] = 'Kc';	
					if ($kqtv['MA_DIEN_AP'] == 0)            $kqtv['CAP_DIEN_AP'] = 'Kc';
					if ($kqtv['CHIEU_DAI'] == 0)            $kqtv['CHIEU_DAI'] = 'Kc';	
					if ($kqtv['CHIEU_RONG'] == 0)            $kqtv['CHIEU_RONG'] = 'Kc';	
					if ($kqtv['CHIEU_CAO'] == 0)            $kqtv['CHIEU_CAO'] = 'Kc';
					if ($kqtv['RUOT_MAY'] == 0)            $kqtv['RUOT_MAY'] = 'Kc';
					if ($kqtv['DAU_MAY'] == 0)            $kqtv['DAU_MAY'] = 'Kc';
					if ($kqtv['TONG'] == 0)            $kqtv['TONG'] = 'Kc';
					if ($kqtv['TEN_LOAI_DAU'] == NULL)            $kqtv['TEN_LOAI_DAU'] = 'Kc';
					 //lay thong tin tram
					$ngayvanhanh = 'Kc';
					$diachitram = 'Kc';
                    $db->setQuery("select * from mba_tram as ct, tram as t where ct.SO_N0 = '" . $mang3[$i] . "' and ct.MA_TRAM = t.MA_TRAM order by ct.NGAY_VAN_HANH desc");
                    $truyvantram = $db->fetchAll();
                    $kqtvtram = mysql_fetch_array($truyvantram);
                    if ($kqtvtram['TEN_TRAM'] == NULL) {
                        $kqtvtram['MA_TRAM'] = 'Chưa lên trạm';
						$kqtvtram['TEN_TRAM'] = 'Kc';
						$ngayvanhanh = 'Kc';
					}
					else {
							 $diachitram = $kqtvtram['DIA_CHI_TRAM'];
							$ngayvanhanh = date('d-m-Y', strtotime($kqtvtram['NGAY_VAN_HANH']));
					}
					//lay thong tin ngung van hanh
					 $ngayngungvh = 'Kc';
                    $db->setQuery("select * from chi_tiet_ngung_van_hanh as ct where ct.SO_N0 = '" . $mang3[$i] . "' order by ct.NGAY_NGUNG_VH desc");
                    $truyvannvh = $db->fetchAll();
                    $kqtvnvh = mysql_fetch_array($truyvannvh);
                    if ($kqtvnvh['NGAY_NGUNG_VH'] != NULL)      
					 {
						$ngayngungvh = date('d-m-Y', strtotime($kqtvnvh['NGAY_NGUNG_VH']));
					}
					if ($kqtv['NAM_SX'] == NULL)         $kqtv['NAM_SX'] = 'Kc';
					if ($kqtv['TEN_NSX'] == NULL)         $kqtv['TEN_NSX'] = 'Kc';
					if ($kqtv['TEN_NUOC'] == NULL)         $kqtv['TEN_NUOC'] = 'Kc';
					if ($kqtv['TEN_LOAI'] == NULL)         $kqtv['TEN_LOAI'] = 'Kc';

					 //lay thong tin dai tu
					$ngaydaitu = 'Kc';
                    $db->setQuery("select * from chi_tiet_dai_tu where SO_N0 = '" . $mang3[$i] . "'  order by NGAY_DAI_TU desc");
                    $truyvandaitu = $db->fetchAll();
                    $kqtvdaitu = mysql_fetch_array($truyvandaitu);
                    if ($kqtvdaitu['NGAY_DAI_TU'] != NULL)                     
					{
						$ngaydaitu = date('d-m-Y', strtotime($kqtvdaitu['NGAY_DAI_TU']));
					}
					 // năm nhập về null ko
                    if ($kqtv['NAM_NHAP_VE'] == NULL)
                        $kqtv['NAM_NHAP_VE'] = 'Kc';
					if ($kqtv['THONG_SO_DO'] == NULL)     $kqtv['THONG_SO_DO'] = 'Kc';
					if ($kqtv['NHAN_XET'] == NULL)        $kqtv['NHAN_XET'] = 'Kc';
					if ($kqtv['GHI_CHU'] == NULL)         $kqtv['GHI_CHU'] = 'Kc';
                    $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('B' . $cellcount, $kqtv['MSTS'])
                            ->setCellValue('C' . $cellcount, $kqtv['SO_N0'])
                            ->setCellValue('D' . $cellcount, $ngaychuyenden)
                            ->setCellValue('E' . $cellcount, $kqtv['CONG_SUAT'])
                            ->setCellValue('F' . $cellcount, $kqtv['CAP_DIEN_AP'])
	                        ->setCellValue('G' . $cellcount, $kqtv['CHIEU_DAI'])
                            ->setCellValue('H' . $cellcount, $kqtv['CHIEU_RONG'])
                            ->setCellValue('I' . $cellcount, $kqtv['CHIEU_CAO'])
                            ->setCellValue('J' . $cellcount, $kqtv['RUOT_MAY'])
                            ->setCellValue('K' . $cellcount, $kqtv['DAU_MAY'])
                            ->setCellValue('L' . $cellcount, $kqtv['TONG'])
                            ->setCellValue('M' . $cellcount, $kqtv['TEN_LOAI_DAU'])
                            ->setCellValue('N' . $cellcount, $kqtvtram['MA_TRAM'])
                            ->setCellValue('O' . $cellcount, $kqtvtram['TEN_TRAM'])
                            ->setCellValue('P' . $cellcount, $diachitram)
                            ->setCellValue('Q' . $cellcount, $ngayvanhanh)
                            ->setCellValue('R' . $cellcount, $ngayngungvh)
							->setCellValue('S' . $cellcount, $kqtv['NAM_SX'])
							->setCellValue('T' . $cellcount, $kqtv['TEN_NSX'])
							->setCellValue('U' . $cellcount, $kqtv['TEN_NUOC'])
							->setCellValue('V' . $cellcount, $kqtv['TEN_LOAI'])

							->setCellValue('W' . $cellcount, $ngaydaitu)
							->setCellValue('X' . $cellcount, $kqtv['NAM_NHAP_VE'])
							->setCellValue('Y' . $cellcount, $kqtv['THONG_SO_DO'])
							->setCellValue('Z' . $cellcount, $kqtv['NHAN_XET'])
							->setCellValue('AA' . $cellcount, $kqtv['GHI_CHU']);
                    $cellcount++;
					}
                }



                $objPHPExcel->getActiveSheet()->mergeCells('A6:AA6');
                $objPHPExcel->getActiveSheet()->mergeCells('A7:AA7');
                $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
                $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('R')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('S')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('T')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('U')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('V')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('W')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('X')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('Y')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('Z')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('AA')->setAutoSize(true);

                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A' . $cellcount, 'Kí Hiệu   Kc: Không có');
                $objPHPExcel->getActiveSheet()->getStyle('A8:' .
                        $objPHPExcel->getActiveSheet()->getHighestColumn() .
                        $objPHPExcel->getActiveSheet()->getHighestRow())->applyFromArray(
                        array(
                            'font' => array(
                                'size' => 12,
                            ),
                            'borders' => array(
                                'allborders' => array(
                                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                                    'color' => array('rgb' => '#000000'),
                                )
                            ),
                            'alignment' => array(
                                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                            )
                        )
                );
// Set format for tableheader
                $objPHPExcel->getActiveSheet()->getStyle('A6:AA7')->applyFromArray(
                        array(
                            'font' => array(
                                'bold' => true,
                                'size' => 13
                            ),
                            'alignment' => array(

                                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                            )
                        )
                );
                $objPHPExcel->getActiveSheet()->mergeCells('A' . $cellcount . ':C' . $cellcount);
            }// het truong hop khong co
        }
    }
}
else {

			$db = new database();
			$mang2 = array(); $k= 0;
			$db->setQuery("select * from don_vi as dv, tai_khoan as tk where dv.MA_DON_VI = tk.MA_DON_VI and  tk.QUYEN <> 2");
			$donvi = $db->fetchAll();				
			while ($rowdonvi= mysql_fetch_array($donvi)){
	//Lay danh sach may don vi tung so huu		
		$db->setQuery("select * from chi_tiet_so_huu where MA_DON_VI = '".$rowdonvi['MA_DON_VI']."' group by SO_N0");
		$dong = $db->fetchAll();
		$mang = array();	$i=0; 

		while ($in = mysql_fetch_array($dong)){
		$mang[$i] = $in['SO_N0'];

		$i++; 

		}

		for($j=0;$j<$i;$j++){
		$db->setQuery("select * from chi_tiet_so_huu where SO_N0 = '".$mang[$j]."' order by NGAY_CHUYEN_DEN desc");
		$truyvan = $db->fetchAll();
		$madvtimdc = mysql_fetch_array($truyvan);

		if ($madvtimdc['MA_DON_VI'] == $rowdonvi['MA_DON_VI']) 
		{
		$mang2[$k]= $madvtimdc['SO_N0'];

		$k++;
		}	
		}	
		
			}//xet tung dv
			
			            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A1', 'Công Ty Điện Lực Cà Mau');

            $objPHPExcel->getActiveSheet()->mergeCells('A1:D1');
			$objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('F1', 'CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM');
			$objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('F2', 'Độc Lập - Tự Do - Hạnh Phúc');

            $objPHPExcel->getActiveSheet()->mergeCells('F1:I1');
			$objPHPExcel->getActiveSheet()->mergeCells('F2:I2');
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A2', 'Địa chỉ: 22 Ngô Quyền P2 TP Cà Mau');
			
            $objPHPExcel->getActiveSheet()->getStyle('A1:I2')->applyFromArray(
                    array(
                        'font' => array(
                            'size' => 13,
                            'bold' => true
									)
						)
            );
			$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->applyFromArray(
                    array(
                        'font' => array(
                            'size' => 12,
							'italic' => true

                        ),
                    )
            );
						$objPHPExcel->getActiveSheet()->getStyle('A1:I2')->applyFromArray(
                    array(
                           'alignment' => array(
                            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                            'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                        )
                    )
            );
            $objPHPExcel->getActiveSheet()->mergeCells('A2:D2');
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('H4', 'Ngày Tạo: ');
			$objPHPExcel->getActiveSheet()->getStyle('H4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			$objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('I4', date('d/m/Y', time()));
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A4', 'Liên hệ: Nguyễn Phạm Thanh Tú/CBKT/Phòng KH-KT');
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A5', 'Điện thoại/Fax/Email: ');
            $objPHPExcel->getActiveSheet()->getStyle('A4:O5')->applyFromArray(
                    array(
                        'font' => array(
                            'size' => 12
                        //'bold' => true
                        ),
                        'alignment' => array(
                            'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
                        )
                    )
            );

    if (count($mang2) == 0) { // Xét xem có sở hữu máy biến áp ko
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A6', 'Không có máy biến áp nào để tạo báo cáo');
    } else {
        // Trường hợp là một tình trạng cụ thể
        if ($tinhtrang != 'khongco') {

            $mang3 = array();
            $dem = 0;
            for ($i = 0; $i < count($mang2); $i++) { //vong for 2
                $db->setQuery("select * from chi_tiet_tt_may where SO_N0 = '" . $mang2[$i] . "' order by TINH_TRANG_NGAY desc");
                $chitiettt = $db->fetchAll();

                $ketqua = mysql_fetch_array($chitiettt);
                if (strtolower($ketqua['TEN_TINH_TRANG_MAY'])== strtolower($tinhtrang)) {
                    $mang3[$dem] = $ketqua['SO_N0'];

                    $dem++;
                }
            }//ket thuc for 2


            if (count($mang3) == 0) { //Kiểm tra đơn vị này ko có máy nào có tình trạng vừa chọn
                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A6', 'Không có máy nào ' . $tinhtrang);
            } else {


                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A6', 'Bảng Báo Cáo Theo Tình Trạng ' . $tinhtrang. ' (dữ liệu mới nhất)')
                        ->setCellValue('A7', 'Bảng Báo Cáo Trên Tất Cả Đơn Vị')
                        ->setCellValue('A8', 'STT')
                        ->setCellValue('B8', 'MSTS')
                        ->setCellValue('C8', 'Số N0')
						->setCellValue('D8', 'Tên Đơn Vị')
                        ->setCellValue('E8', 'Ngày Chuyển Đến')
                        ->setCellValue('F8', 'S (kVA)')
                        ->setCellValue('G8', 'U (kV)')
						->setCellValue('H8', 'Chiều Dài (mm)')
						->setCellValue('I8', 'Chiều Rộng (mm)')
						->setCellValue('J8', 'Chiều Cao (mm)')						
						->setCellValue('K8', 'Ruột Máy (kg)')
						->setCellValue('L8', 'Dầu (kg)')
						->setCellValue('M8', 'Tổng (kg)')
						->setCellValue('N8', 'Loại Dầu')
						->setCellValue('O8', 'Mã Trạm')
                        ->setCellValue('P8', 'Tên Trạm')
						->setCellValue('Q8', 'Địa Chỉ Trạm')
                        ->setCellValue('R8', 'Ngày Vận Hành')
						->setCellValue('S8', 'Ngày Ngừng Vận Hành')
                        ->setCellValue('T8', 'Năm Sản Xuất')
                        ->setCellValue('U8', 'Nhà Sản Xuất')
						->setCellValue('V8', 'Nước Sản Xuất')
                        ->setCellValue('W8', 'Loại Máy')
                        ->setCellValue('X8', 'Tình Trạng Ngày')
                        ->setCellValue('Y8', 'Chi Tiết Tình Trạng')
                        ->setCellValue('Z8', 'Ngày Dại Tu')
						->setCellValue('AA8', 'Năm Nhập Về')
                        ->setCellValue('AB8', 'Thông Số Đo')
                        ->setCellValue('AC8', 'Nhận Xét')
                        ->setCellValue('AD8', 'Ghi Chú');


                $stt = 1;
                $cellcount = 9;
                for ($i = 0; $i < count($mang3); $i++) {
                    $db->setQuery("select * from may_bien_ap as mba, loai_may as loai , nha_sx as nsx, loai_dien_ap as loaida where mba.SO_N0 = '" . $mang3[$i] . "' and mba.MA_NSX = nsx.MA_NSX and loai.MA_LOAI = mba.MA_LOAI and loaida.MA_DIEN_AP = mba.MA_DIEN_AP");
                    $truyvan = $db->fetchAll();
					if (mysql_num_rows($truyvan) != 0) {
                    $kqtv = mysql_fetch_array($truyvan);


                    $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('A' . $cellcount, $stt);
                    $stt++; //1
                    //kt msts có null hay ko
                    if ($kqtv['MSTS'] == NULL)
                        $kqtv['MSTS'] = 'Kc';
                    //lay ngay chuyen den
					$ngaychuyenden = 'Kc';
					$db->setQuery("select * from chi_tiet_so_huu as ctsh, don_vi as dv where ctsh.SO_N0 = '" . $mang3[$i] . "' and ctsh.MA_DON_VI = dv.MA_DON_VI order by NGAY_CHUYEN_DEN desc");
                    $truyvansohuu = $db->fetchAll();
                    $kqtvsh = mysql_fetch_array($truyvansohuu);
					if ($kqtvsh['NGAY_CHUYEN_DEN'] !=  NULL) {
						$ngaychuyenden = date('d-m-Y', strtotime($kqtvsh['NGAY_CHUYEN_DEN']));
					}
					if ($kqtv['CONG_SUAT'] == 0)            $kqtv['CONG_SUAT'] = 'Kc';	
					if ($kqtv['MA_DIEN_AP'] == 0)            $kqtv['CAP_DIEN_AP'] = 'Kc';
					if ($kqtv['CHIEU_DAI'] == 0)            $kqtv['CHIEU_DAI'] = 'Kc';	
					if ($kqtv['CHIEU_RONG'] == 0)            $kqtv['CHIEU_RONG'] = 'Kc';	
					if ($kqtv['CHIEU_CAO'] == 0)            $kqtv['CHIEU_CAO'] = 'Kc';
					if ($kqtv['RUOT_MAY'] == 0)            $kqtv['RUOT_MAY'] = 'Kc';
					if ($kqtv['DAU_MAY'] == 0)            $kqtv['DAU_MAY'] = 'Kc';
					if ($kqtv['TONG'] == 0)            $kqtv['TONG'] = 'Kc';
					if ($kqtv['TEN_LOAI_DAU'] == NULL)            $kqtv['TEN_LOAI_DAU'] = 'Kc';
					 //lay thong tin tram
					$ngayvanhanh = 'Kc';
					$diachitram = 'Kc';
                    $db->setQuery("select * from mba_tram as ct, tram as t where ct.SO_N0 = '" . $mang3[$i] . "' and ct.MA_TRAM = t.MA_TRAM order by ct.NGAY_VAN_HANH desc");
                    $truyvantram = $db->fetchAll();
                    $kqtvtram = mysql_fetch_array($truyvantram);
                    if ($kqtvtram['TEN_TRAM'] == NULL) {
                        $kqtvtram['MA_TRAM'] = 'Chưa lên trạm';
						$kqtvtram['TEN_TRAM'] = 'Kc';
						$ngayvanhanh = 'Kc';
					}
					else {
							 $diachitram = $kqtvtram['DIA_CHI_TRAM'];
							$ngayvanhanh = date('d-m-Y', strtotime($kqtvtram['NGAY_VAN_HANH']));
					}
					//lay thong tin ngung van hanh
					 $ngayngungvh = 'Kc';
                    $db->setQuery("select * from chi_tiet_ngung_van_hanh as ct where ct.SO_N0 = '" . $mang3[$i] . "' order by ct.NGAY_NGUNG_VH desc");
                    $truyvannvh = $db->fetchAll();
                    $kqtvnvh = mysql_fetch_array($truyvannvh);
                    if ($kqtvnvh['NGAY_NGUNG_VH'] != NULL)      
					 {
						$ngayngungvh = date('d-m-Y', strtotime($kqtvnvh['NGAY_NGUNG_VH']));
					}
					if ($kqtv['NAM_SX'] == NULL)         $kqtv['NAM_SX'] = 'Kc';
					if ($kqtv['TEN_NSX'] == NULL)         $kqtv['TEN_NSX'] = 'Kc';
					if ($kqtv['TEN_NUOC'] == NULL)         $kqtv['TEN_NUOC'] = 'Kc';
					if ($kqtv['TEN_LOAI'] == NULL)         $kqtv['TEN_LOAI'] = 'Kc';
					$db->setQuery("select * from chi_tiet_tt_may where SO_N0 = '" . $mang3[$i] . "' order by TINH_TRANG_NGAY desc");
                    $truyvantt = $db->fetchAll();
                    $kqtvtt = mysql_fetch_array($truyvantt);
					$tinhtrangngay = date('d-m-Y', strtotime($kqtvtt['TINH_TRANG_NGAY']));
					 if ($kqtvtt['DG_TINH_TRANG'] == NULL)
                        $kqtvtt['DG_TINH_TRANG'] = 'Kc';
					 //lay thong tin dai tu
					$ngaydaitu = 'Kc';
                    $db->setQuery("select * from chi_tiet_dai_tu where SO_N0 = '" . $mang3[$i] . "'  order by NGAY_DAI_TU desc");
                    $truyvandaitu = $db->fetchAll();
                    $kqtvdaitu = mysql_fetch_array($truyvandaitu);
                    if ($kqtvdaitu['NGAY_DAI_TU'] != NULL)                     
					{
						$ngaydaitu = date('d-m-Y', strtotime($kqtvdaitu['NGAY_DAI_TU']));
					}
					// năm nhập về null ko
                    if ($kqtv['NAM_NHAP_VE'] == NULL)     $kqtv['NAM_NHAP_VE'] = 'Kc';
					if ($kqtv['THONG_SO_DO'] == NULL)     $kqtv['THONG_SO_DO'] = 'Kc';
					if ($kqtv['NHAN_XET'] == NULL)        $kqtv['NHAN_XET'] = 'Kc';
					if ($kqtv['GHI_CHU'] == NULL)         $kqtv['GHI_CHU'] = 'Kc';
                    $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('B' . $cellcount, $kqtv['MSTS'])
                            ->setCellValue('C' . $cellcount, $kqtv['SO_N0'])
							->setCellValue('D' . $cellcount, $kqtvsh['TEN_DON_VI'])
                            ->setCellValue('E' . $cellcount, $ngaychuyenden)
                            ->setCellValue('F' . $cellcount, $kqtv['CONG_SUAT'])
                            ->setCellValue('G' . $cellcount, $kqtv['CAP_DIEN_AP'])
	                        ->setCellValue('H' . $cellcount, $kqtv['CHIEU_DAI'])
                            ->setCellValue('I' . $cellcount, $kqtv['CHIEU_RONG'])
                            ->setCellValue('J' . $cellcount, $kqtv['CHIEU_CAO'])
                            ->setCellValue('K' . $cellcount, $kqtv['RUOT_MAY'])
                            ->setCellValue('L' . $cellcount, $kqtv['DAU_MAY'])
                            ->setCellValue('M' . $cellcount, $kqtv['TONG'])
                            ->setCellValue('N' . $cellcount, $kqtv['TEN_LOAI_DAU'])
                            ->setCellValue('O' . $cellcount, $kqtvtram['MA_TRAM'])
                            ->setCellValue('P' . $cellcount, $kqtvtram['TEN_TRAM'])
                            ->setCellValue('Q' . $cellcount, $diachitram)
                            ->setCellValue('R' . $cellcount, $ngayvanhanh)
                            ->setCellValue('S' . $cellcount, $ngayngungvh)
							->setCellValue('T' . $cellcount, $kqtv['NAM_SX'])
							->setCellValue('U' . $cellcount, $kqtv['TEN_NSX'])
							->setCellValue('V' . $cellcount, $kqtv['TEN_NUOC'])
							->setCellValue('W' . $cellcount, $kqtv['TEN_LOAI'])
							->setCellValue('X' . $cellcount, $tinhtrangngay)
							->setCellValue('Y' . $cellcount, $kqtvtt['DG_TINH_TRANG'])
							->setCellValue('Z' . $cellcount, $ngaydaitu)
							->setCellValue('AA' . $cellcount, $kqtv['NAM_NHAP_VE'])
							->setCellValue('AB' . $cellcount, $kqtv['THONG_SO_DO'])
							->setCellValue('AC' . $cellcount, $kqtv['NHAN_XET'])
							->setCellValue('AD' . $cellcount, $kqtv['GHI_CHU']);
                    $cellcount++;
					}
                }

                $objPHPExcel->getActiveSheet()->mergeCells('A6:AD6');
                $objPHPExcel->getActiveSheet()->mergeCells('A7:AD7');
                $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
                $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('R')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('S')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('T')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('U')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('V')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('W')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('X')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('Y')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('Z')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('AA')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('AB')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('AC')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('AD')->setAutoSize(true);
                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A' . $cellcount, 'Kí Hiệu   Kc: Không có');
                $objPHPExcel->getActiveSheet()->getStyle('A8:' .
                        $objPHPExcel->getActiveSheet()->getHighestColumn() .
                        $objPHPExcel->getActiveSheet()->getHighestRow())->applyFromArray(
                        array(
                            'font' => array(
                                'size' => 12
                            ),
                            'borders' => array(
                                'allborders' => array(
                                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                                    'color' => array('rgb' => '#000000'),
                                )
                            ),
                            'alignment' => array(
                                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                            )
                        )
                );
// Set format for tableheader
                $objPHPExcel->getActiveSheet()->getStyle('A6:AD7')->applyFromArray(
                        array(
                            'font' => array(
                                'bold' => true,
                                'size' => 13

                            ),
                            'alignment' => array(

                                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                            )
                        )
                );
                $objPHPExcel->getActiveSheet()->mergeCells('A' . $cellcount . ':D' . $cellcount);
            } //kt ko co tinh trạng bao cao
        }// het if của có tình trạng
        else
        // Chọn tình trạng là không có
        if ($tinhtrang == 'khongco') {
            $dem = 0;
            $mang3 = array();
            for ($i = 0; $i < count($mang2); $i++) { //vong for 2
                $db->setQuery("select * from chi_tiet_tt_may where SO_N0 = '" . $mang2[$i] . "'");
                $chitiettt = $db->fetchAll();
                $ketqua = mysql_fetch_array($chitiettt);
                //$kt = mysqli_num_rows($ketqua);
                if ($ketqua['SO_N0'] == NULL) {
                    $mang3[$dem] = $mang2[$i];
                    $dem++;
                }
            }


            //Bat dau xu ly de tao bao cao

            if (count($mang3) == 0) { // Kiểm tra đơn vị có máy nào không có tình trạng hay ko
                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A6', 'Không có máy nào không có tình trạng');
                //echo "Đơn vị ".$tendv." không có máy không có tình trạng ";
            } else {
$objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A6', 'Bảng Báo Cáo Cho Các Máy Chưa Nhập Tình Trạng (dữ liệu mới nhất)')
                        ->setCellValue('A7', 'Bảng Báo Cáo Trên Tất Cả Đơn Vị')
                        ->setCellValue('A8', 'STT')
                        ->setCellValue('B8', 'MSTS')
                        ->setCellValue('C8', 'Số N0')
						->setCellValue('D8', 'Tên Đơn Vị')
                        ->setCellValue('E8', 'Ngày Chuyển Đến')
                        ->setCellValue('F8', 'S (kVA)')
                        ->setCellValue('G8', 'U (kV)')
						->setCellValue('H8', 'Chiều Dài (mm)')
						->setCellValue('I8', 'Chiều Rộng (mm)')
						->setCellValue('J8', 'Chiều Cao (mm)')						
						->setCellValue('K8', 'Ruột Máy (kg)')
						->setCellValue('L8', 'Dầu (kg)')
						->setCellValue('M8', 'Tổng (kg)')
						->setCellValue('N8', 'Loại Dầu')
						->setCellValue('O8', 'Mã Trạm')
                        ->setCellValue('P8', 'Tên Trạm')
						->setCellValue('Q8', 'Địa Chỉ Trạm')
                        ->setCellValue('R8', 'Ngày Vận Hành')
						->setCellValue('S8', 'Ngày Ngừng Vận Hành')
                        ->setCellValue('T8', 'Năm Sản Xuất')
                        ->setCellValue('U8', 'Nhà Sản Xuất')
						->setCellValue('V8', 'Nước Sản Xuất')
                        ->setCellValue('W8', 'Loại Máy')
                        ->setCellValue('X8', 'Ngày Đại Tu')
						->setCellValue('Y8', 'Năm Nhập Về')
                        ->setCellValue('Z8', 'Thông Số Đo')
                        ->setCellValue('AA8', 'Nhận Xét')
                        ->setCellValue('AB8', 'Ghi Chú');
                $stt = 1;
                $cellcount = 9;
                for ($i = 0; $i < count($mang3); $i++) {
                    $db->setQuery("select * from may_bien_ap as mba, loai_may as loai , nha_sx as nsx, loai_dien_ap as loaida where mba.SO_N0 = '" . $mang3[$i] . "' and mba.MA_NSX = nsx.MA_NSX and loai.MA_LOAI = mba.MA_LOAI and loaida.MA_DIEN_AP = mba.MA_DIEN_AP");
                    $truyvan = $db->fetchAll();
					if (mysql_num_rows($truyvan) != 0) {
                    $kqtv = mysql_fetch_array($truyvan);
                    $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('A' . $cellcount, $stt);
                    $stt++; //1
                    //kt msts có null hay ko
                    if ($kqtv['MSTS'] == NULL)
                        $kqtv['MSTS'] = 'Kc';
                    //lay ngay chuyen den
					$ngaychuyenden = 'Kc';
					$db->setQuery("select * from chi_tiet_so_huu as ctsh, don_vi as dv where ctsh.SO_N0 = '" . $mang3[$i] . "' and ctsh.MA_DON_VI = dv.MA_DON_VI order by NGAY_CHUYEN_DEN desc");
                    $truyvansohuu = $db->fetchAll();
                    $kqtvsh = mysql_fetch_array($truyvansohuu);
					if ($kqtvsh['NGAY_CHUYEN_DEN'] !=  NULL) {
						$ngaychuyenden = date('d-m-Y', strtotime($kqtvsh['NGAY_CHUYEN_DEN']));
					}

					if ($kqtv['CHIEU_DAI'] == 0)            $kqtv['CHIEU_DAI'] = 'Kc';	
					if ($kqtv['CHIEU_RONG'] == 0)            $kqtv['CHIEU_RONG'] = 'Kc';	
					if ($kqtv['CHIEU_CAO'] == 0)            $kqtv['CHIEU_CAO'] = 'Kc';
					if ($kqtv['RUOT_MAY'] == 0)            $kqtv['RUOT_MAY'] = 'Kc';
					if ($kqtv['DAU_MAY'] == 0)            $kqtv['DAU_MAY'] = 'Kc';
					if ($kqtv['TONG'] == 0)            $kqtv['TONG'] = 'Kc';
					if ($kqtv['TEN_LOAI_DAU'] == NULL)            $kqtv['TEN_LOAI_DAU'] = 'Kc';
					 //lay thong tin tram
					$ngayvanhanh = 'Kc';
					$diachitram = 'Kc';
                    $db->setQuery("select * from mba_tram as ct, tram as t where ct.SO_N0 = '" . $mang3[$i] . "' and ct.MA_TRAM = t.MA_TRAM order by ct.NGAY_VAN_HANH desc");
                    $truyvantram = $db->fetchAll();
                    $kqtvtram = mysql_fetch_array($truyvantram);
                    if ($kqtvtram['TEN_TRAM'] == NULL) {
                        $kqtvtram['MA_TRAM'] = 'Chưa lên trạm';
						$kqtvtram['TEN_TRAM'] = 'Kc';
						$ngayvanhanh = 'Kc';
					}
					else {
							 $diachitram = $kqtvtram['DIA_CHI_TRAM'];
							$ngayvanhanh = date('d-m-Y', strtotime($kqtvtram['NGAY_VAN_HANH']));
					}
					//lay thong tin ngung van hanh
					 $ngayngungvh = 'Kc';
                    $db->setQuery("select * from chi_tiet_ngung_van_hanh as ct where ct.SO_N0 = '" . $mang3[$i] . "' order by ct.NGAY_NGUNG_VH desc");
                    $truyvannvh = $db->fetchAll();
                    $kqtvnvh = mysql_fetch_array($truyvannvh);
                    if ($kqtvnvh['NGAY_NGUNG_VH'] != NULL)      
					 {
						$ngayngungvh = date('d-m-Y', strtotime($kqtvnvh['NGAY_NGUNG_VH']));
					}
					if ($kqtv['NAM_SX'] == NULL)         $kqtv['NAM_SX'] = 'Kc';
					if ($kqtv['TEN_NSX'] == NULL)         $kqtv['TEN_NSX'] = 'Kc';
					if ($kqtv['TEN_NUOC'] == NULL)         $kqtv['TEN_NUOC'] = 'Kc';
					if ($kqtv['TEN_LOAI'] == NULL)         $kqtv['TEN_LOAI'] = 'Kc';

					 //lay thong tin dai tu
					$ngaydaitu = 'Kc';
                    $db->setQuery("select * from chi_tiet_dai_tu where SO_N0 = '" . $mang3[$i] . "'  order by NGAY_DAI_TU desc");
                    $truyvandaitu = $db->fetchAll();
                    $kqtvdaitu = mysql_fetch_array($truyvandaitu);
                    if ($kqtvdaitu['NGAY_DAI_TU'] != NULL)                     
					{
						$ngaydaitu = date('d-m-Y', strtotime($kqtvdaitu['NGAY_DAI_TU']));
					}
					// năm nhập về null ko
                    if ($kqtv['NAM_NHAP_VE'] == NULL)     $kqtv['NAM_NHAP_VE'] = 'Kc';
					if ($kqtv['THONG_SO_DO'] == NULL)     $kqtv['THONG_SO_DO'] = 'Kc';
					if ($kqtv['NHAN_XET'] == NULL)        $kqtv['NHAN_XET'] = 'Kc';
					if ($kqtv['GHI_CHU'] == NULL)         $kqtv['GHI_CHU'] = 'Kc';
                    $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('B' . $cellcount, $kqtv['MSTS'])
                            ->setCellValue('C' . $cellcount, $kqtv['SO_N0'])
							->setCellValue('D' . $cellcount, $kqtvsh['TEN_DON_VI'])
                            ->setCellValue('E' . $cellcount, $ngaychuyenden)
                            ->setCellValue('F' . $cellcount, $kqtv['CONG_SUAT'])
                            ->setCellValue('G' . $cellcount, $kqtv['CAP_DIEN_AP'])
	                        ->setCellValue('H' . $cellcount, $kqtv['CHIEU_DAI'])
                            ->setCellValue('I' . $cellcount, $kqtv['CHIEU_RONG'])
                            ->setCellValue('J' . $cellcount, $kqtv['CHIEU_CAO'])
                            ->setCellValue('K' . $cellcount, $kqtv['RUOT_MAY'])
                            ->setCellValue('L' . $cellcount, $kqtv['DAU_MAY'])
                            ->setCellValue('M' . $cellcount, $kqtv['TONG'])
                            ->setCellValue('N' . $cellcount, $kqtv['TEN_LOAI_DAU'])
                            ->setCellValue('O' . $cellcount, $kqtvtram['MA_TRAM'])
                            ->setCellValue('P' . $cellcount, $kqtvtram['TEN_TRAM'])
                            ->setCellValue('Q' . $cellcount, $diachitram)
                            ->setCellValue('R' . $cellcount, $ngayvanhanh)
                            ->setCellValue('S' . $cellcount, $ngayngungvh)
							->setCellValue('T' . $cellcount, $kqtv['NAM_SX'])
							->setCellValue('U' . $cellcount, $kqtv['TEN_NSX'])
							->setCellValue('V' . $cellcount, $kqtv['TEN_NUOC'])
							->setCellValue('W' . $cellcount, $kqtv['TEN_LOAI'])
							->setCellValue('X' . $cellcount, $ngaydaitu)
							->setCellValue('Y' . $cellcount, $kqtv['NAM_NHAP_VE'])
							->setCellValue('Z' . $cellcount, $kqtv['THONG_SO_DO'])
							->setCellValue('AA' . $cellcount, $kqtv['NHAN_XET'])
							->setCellValue('AB' . $cellcount, $kqtv['GHI_CHU']);
                    $cellcount++;
					}
                }



                $objPHPExcel->getActiveSheet()->mergeCells('A6:AB6');
                $objPHPExcel->getActiveSheet()->mergeCells('A7:AB7');
                $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
                $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('R')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('S')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('T')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('U')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('V')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('W')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('X')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('Y')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('Z')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('AA')->setAutoSize(true);
				$objPHPExcel->getActiveSheet()->getColumnDimension('AB')->setAutoSize(true);


                $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A' . $cellcount, 'Kí Hiệu   Kc: Không có');
                $objPHPExcel->getActiveSheet()->getStyle('A8:' .
                        $objPHPExcel->getActiveSheet()->getHighestColumn() .
                        $objPHPExcel->getActiveSheet()->getHighestRow())->applyFromArray(
                        array(
                            'font' => array(
                                'size' => 12
                            ),
                            'borders' => array(
                                'allborders' => array(
                                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                                    'color' => array('rgb' => '#000000'),
                                )
                            ),
                            'alignment' => array(
                                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                            )
                        )
                );
// Set format for tableheader
                $objPHPExcel->getActiveSheet()->getStyle('A6:AB7')->applyFromArray(
                        array(
                            'font' => array(
                                'bold' => true,
                                'size' => 13
                            ),
                            'alignment' => array(

                                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                            )
                        )
                );
                $objPHPExcel->getActiveSheet()->mergeCells('A' . $cellcount . ':C' . $cellcount);
            }// het truong hop khong co
        }
    }
}
		// dinh font cho toan bo file
   		 $objPHPExcel->getActiveSheet()->getStyle('A1:' .
            $objPHPExcel->getActiveSheet()->getHighestColumn() .
            $objPHPExcel->getActiveSheet()->getHighestRow())->applyFromArray(
            array(
                'font' => array(                    
                    'name' => 'Times new Roman'
                )

            )
    );
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Báo cáo');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Baocao.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>